package com.feeyo.groundservice.base_util

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager

/**
 *作者：70893 on 2025-09-21
 *描述：
 */
/**切换页面
 *
 * @param index 需要切换的页面索引
 * **/
fun switchPages(
    mFragmentManager: FragmentManager?,
    mFragments: List<Fragment>,
    mContainerViewId: Int?,
    index: Int
) {
    mFragmentManager?.let { fragmentManager ->
        val fragmentTransaction = fragmentManager.beginTransaction()
        var fragment: Fragment
        for (i in mFragments.indices) {
            if (i == index) {
                continue
            }
            fragment = mFragments[i]
            if (fragment.isAdded) {
                fragmentTransaction.hide(fragment)
            }
        }
        fragment = mFragments[index]
        if (fragment.isAdded) {
            fragmentTransaction.show(fragment)
        } else {
            mContainerViewId?.let {
                fragmentTransaction.add(it, fragment)
            }
        }
        fragmentTransaction.commitAllowingStateLoss()
    }
}